<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" title="Default" href="main.css" type="text/css" />
</head>

<body>
<h2 align="center">Command Settings</h2>
<hr/>
<?
my $gets = $request->get_all;
my $error=0;
if (defined($gets->{"update.x"})) {
	my $i=0;
	my $max=$gets->{maxid};
	$EQDB->query("delete from commands");
	for(my $i=0;$i<$max;++$i) {
		my ($command,$access) = ($gets->{"command$i"},$gets->{"access$i"});
		next if (!$command);
		$q=sprintf("replace into commands (command,access) values('%s',%s)",$EQDB->escape_string($command),$EQDB->escape_string($access));
		$EQDB->query($q);
		if ($EQDB->get_errno) {
			$error=1;
			printf("Error adding command %s: %s<br>\n",$command,$EQDB->error);
		}
	}
	if ($error==0) {
		printf("<h3>Command settings updated, will take affect on zone restart.</h3><br>\n");
	}
}
?>
<form method="get" action="commands.html">
<?
	my $i=0;
	$q="select * from commands order by command";
	my $res=$EQDB->query($q);
	if ($res) { 
		print "<table id=\"cmd_tab\" border=\"1\" cellspacing=\"2\" cellpadding=\"3\" class=\"zonelist\"><tr>\n";
		printf "\t\t<th scope=\"col\" width=\"150\">Command</th>\n";
		printf "\t\t<th scope=\"col\" width=\"70\">Access</th>\n";
		printf "\t</tr>\n";

		while(my $row=$res->fetch_row_hash) {
			printf "\t\t<tr><td>#%s<input id=\"cmd%d\" type=\"hidden\" name=\"command%d\" value=\"%s\"></td>\n",$row->{command},$i,$i,$row->{command};
			printf "\t\t<td><input size=\"4\" type=\"text\" value=\"%s\" name=\"access%d\"/>&nbsp;<img src=\"delete.gif\" alt=\"delete\" onClick=\"del_row(%d)\"></td>\n",$row->{access},$i,$i;
			print "\t</tr>\n";
			++$i;
		}
	}
	printf("<tr><td colspan=\"2\" align=\"center\"><input value=\"Add New Command\" type=button onClick=\"add_row()\"/></td></tr>\n");
	printf("</table><script language=\"javascript\">var next_field=%d;</script>\n",$i);
        printf("<input type=\"hidden\" id=\"maxid\" value=\"%d\" name=\"maxid\">\n",$i-1);

?>
<script language="javascript">
function add_row() {
	var tab=document.getElementById('cmd_tab');
	var row=tab.insertRow(tab.rows.length-1);

	var cell=row.insertCell(0);
	var textNode = document.createTextNode('#');
	cell.appendChild(textNode);

	var el=document.createElement('input');
	el.type='text';
	el.name='command'+next_field;
	el.value='NewCommand';
	el.id='cmd'+next_field;
	el.size=20;
	cell.appendChild(el);

	cell=row.insertCell(1);
	el=document.createElement('input');
	el.type='text';
	el.name='access'+next_field;
	el.value='100';
	el.size=4;
	cell.appendChild(el);

	el=document.createElement('img');
	el.src='delete.gif';
	el.onclick=new Function('del_row('+next_field+')');
	cell.appendChild(el);

	++next_field;
        var inp=document.getElementById('maxid');
        inp.value=next_field;

}
function del_row(i) {
	var row=document.getElementById('cmd'+i);
	row.parentNode.parentNode.parentNode.removeChild(row.parentNode.parentNode);
}
</script>
<p>
<input type="image" src="update.png" name="update"/>
<input type="image" src="reset.png"  name="reset"/>
</form>
</body>
</html>

